package com.whiteseason.whitebot.plugin;

import com.whiteseason.whitebot.dao.UserRepository;
import com.whiteseason.whitebot.pojo.User;
import com.whiteseason.whitebot.pojo.UserInfo;
import com.whiteseason.whitebot.util.OSU.UserGet;
import lombok.extern.slf4j.Slf4j;
import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.event.message.CQPrivateMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;

@Component
@Slf4j
public class RegisterPlugin extends CQPlugin {
    @Autowired
    UserGet userGet;
    @Autowired
    UserRepository userRepository;
    @Override
    public int onPrivateMessage(CoolQ cq, CQPrivateMessageEvent event) {
        String msg = event.getMessage();
        if(msg.startsWith("setname")){
            if (msg.equals("setname") || !msg.substring(7).startsWith(" ")) {
                cq.sendPrivateMsg(event.getUserId(),
                        "请按照格式要求输入，例如：!setname whtiebot",false);
                return MESSAGE_BLOCK;
            }
            String[] handlemsg = msg.split(" ",2);
            if(userRepository.findByQq(Long.toString(event.getUserId())) != null){
                cq.sendPrivateMsg(event.getUserId(),
                        "该QQ已绑定玩家，如需更改绑定请联系管理",false);
                return MESSAGE_BLOCK;
            }
            try {
                UserInfo userInfo = userGet.getUser(handlemsg[1], 0);
                if(userInfo == null){
                    cq.sendPrivateMsg(event.getUserId(),"玩家信息查询失败，请检查昵称是否正确",false);
                    return MESSAGE_BLOCK;
                }
                User user = new User(userInfo,Long.toString(event.getUserId()));
                userRepository.save(user);
                cq.sendPrivateMsg(event.getUserId(),"QQ号："+event.getUserId()+"成功绑定为"+
                        userInfo.getUsername(),false);
            }catch (RestClientException clientException){
                //网络异常捕获
                cq.sendPrivateMsg(event.getUserId(),"网络异常，请重试或联系白季",false);
                log.error("获取用户信息网络异常",clientException);
            }
            return MESSAGE_BLOCK;
        }
        return MESSAGE_IGNORE;
    }

    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        String msg = event.getMessage();
        if(msg.startsWith("setname")){
            if (msg.equals("setname") || !msg.substring(7).startsWith(" ")) {
                cq.sendGroupMsg(event.getGroupId(),
                        "请按照格式要求输入，例如：!setname whtiebot",false);
                return MESSAGE_BLOCK;
            }
            String[] handlemsg = msg.split(" ",2);
            if(userRepository.findByQq(Long.toString(event.getUserId())) != null){
                cq.sendGroupMsg(event.getGroupId(),
                        "该QQ已绑定玩家，如需更改绑定请联系管理",false);
                return MESSAGE_BLOCK;
            }
            try {
                UserInfo userInfo = userGet.getUser(handlemsg[1], 0);
                if(userInfo == null){
                    cq.sendGroupMsg(event.getGroupId(),"玩家信息查询失败，请检查昵称是否正确",false);
                    return MESSAGE_BLOCK;
                }
                User user = new User(userInfo,Long.toString(event.getUserId()));
                userRepository.save(user);
            }catch (RestClientException clientException){
                cq.sendGroupMsg(event.getGroupId(),"网络异常，请重试或联系白季",false);
                log.error("获取用户信息网络异常",clientException);
                return MESSAGE_BLOCK;
            }
            cq.sendGroupMsg(event.getGroupId(),"QQ号："+event.getUserId()+"成功绑定为"+
                    handlemsg[1],false);
            return MESSAGE_BLOCK;
        }
        return MESSAGE_IGNORE;
    }
}
